package com.hospital.hospital.controller;


import com.hospital.hospital.bean.Department;
import com.hospital.hospital.bean.Doctor;
import com.hospital.hospital.service.DoctorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;

import java.util.List;


@Controller
@SessionAttributes(types = {Doctor.class})
public class DoctorController {

    @Autowired
    private DoctorService doctorService;


    @RequestMapping("/doctormanager")
    public String doctormanager() {

        return "doctormanager";
    }

    @RequestMapping("/ggggUi")
    public String ggggUi() {

        return "gggg";
    }

    @RequestMapping("/hhhhhUi")
    public String hhhhhUi(Model model) {
        try {
            List<Doctor> doctors = doctorService.queryAll();
            model.addAttribute("doctors", doctors);


        } catch (Exception e) {
            e.printStackTrace();
        }
        return "hhhhh";
    }

    @RequestMapping("/insertdoctorUi")
    public String insertdoctorUi() {
        return "insertdoctor";
    }

    @RequestMapping("/insertdoctor")
    public String insert(Model model, Doctor doctor) {

        try {
            doctorService.insert(doctor);
        } catch (Exception e) {
            e.printStackTrace();
        }

            model.addAttribute("doctor", doctor);

        return "redirect:/hhhhhUi";
    }

    @RequestMapping("/deleteBydoctorId")
    public String deleteBydoctorId(Integer[] doctorIds ,Model model) {

        for (Integer doctorId:doctorIds) {
            System.out.println(doctorId);
            try {
                doctorService.deleteByDoctorId(doctorId);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }


        List<Doctor> doctors = null;
        try {
             doctors = doctorService.queryAll();
        } catch (Exception e) {
            e.printStackTrace();
        }
        model.addAttribute("doctors",doctors);
        return "redirect:/hhhhhUi";
    }

    @RequestMapping("/updateUi")
    public String updateUi(Integer doctorIds,Model model) {
        System.out.println(doctorIds);
        model.addAttribute("doctorId" ,doctorIds);
        return "update";
    }

    @RequestMapping("/updateById")
    public String updateById(Doctor doctor) {
        System.out.println(doctor);
        try {
            doctorService.updateById(doctor);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "redirect:/hhhhhUi";
    }


    @RequestMapping("/query")
    public String query(String likename,Model model) throws Exception {
        List<Doctor> doctors = doctorService.queryLike(likename);
        model.addAttribute("doctors",doctors);
        return "query";
    }

    @RequestMapping("/queryAll")
    public String queryAll(Model model) throws Exception {
        List<Doctor> doctors = doctorService.queryAll();
        model.addAttribute("doctors",doctors);
        return "redirect:/hhhhhUi";
    }

    @RequestMapping("/second")
    public String second(Integer page,Model model){
        System.out.println(page);
        List<Doctor> doctors = doctorService.queryByPage(page);
        model.addAttribute("doctors",doctors);
        return "hhhhh";
    }
}